local extension = Package("lzhou_sg")
extension.extensionName = "tshu"

Fk:loadTranslationTable{
  ["lzhou_sg"] = "龙舟.双鼓",
  ["tshulz"] = "龙舟",
}

local U = require "packages/utility/utility"

local zuogu = General(extension, "tshulz__zuogu","qun",10)
zuogu.hidden = true
local jisheng = fk.CreateTriggerSkill{
    name = "tshu__jisheng",
    anim_type = "defensive",
    priority = 2,
    events = {fk.EventPhaseChanging,fk.Damaged,fk.DamageInflicted,fk.Death},
    frequency = Skill.Compulsory,
    can_trigger = function(self, event, target, player, data)
      if event == fk.EventPhaseChanging then
        return target == player and player:hasSkill(self)  and data.to == Player.Draw
      elseif event == fk.Damaged  then
        return target == player and player:hasSkill(self,true,true) and player.room:getTag("RoundCount")%2 ~= 0
      elseif event == fk.DamageInflicted then
        return target == player and player:hasSkill(self) and player.room:getTag("RoundCount")%2 == 0
      end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
      if event == fk.EventPhaseChanging then
        return true
      elseif event == fk.Damaged  then
        local n =  data.damage
        if n < 1 then return end
        for i = 1, n, 1 do
            for _, p in ipairs(room.alive_players) do
                if p.role == "lord" then
                    room:loseHp(p,1,self.name)
                    break
                end
            end
            local lz = {"lz_draw","lz_damage","lz_cover","lz_control"}
            local glz = {}
            for _, m in ipairs(lz) do
                if player:getMark(m.."-turn") < 9 then
                    table.insertIfNeed(glz,m)
                end
            end
            if #glz > 0 then
                local m = table.random(glz)
                room:addPlayerMark(player,m.."-turn",2)
                room:setBanner("@lzjd",((tonumber(string.match(room:getBanner("@lzjd") or 0, "%d+"))) + 2).."米")
                room:setBanner("lzjd",(room:getBanner("lzjd") or 0) + 2)
                room:setBanner(m,(room:getBanner(m) or 0) + 2)
                room.logic:trigger("fk.lzjd", player, { name = "@lzjd", num = 2})
                
            end
        end
      elseif event == fk.DamageInflicted then
        return true
      end
    end,
}
local zuogus = fk.CreateTriggerSkill{
    name = "tshu__zuogu",
    anim_type = "defensive",
    events = {fk.Damaged},
    frequency = Skill.Compulsory,
    can_trigger = function(self, event, target, player, data)
      return target == player and player:hasSkill(self) and data.from and not data.from.dead
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        if  player:usedSkillTimes(self.name, Player.HistoryTurn) == 1 then
            data.from:drawCards(2,self.name)
        elseif  player:usedSkillTimes(self.name, Player.HistoryTurn) == 2 then
            if not data.from:isNude() then
                room:askForDiscard(data.from,2,2,true,self.name,false)
            end
        elseif player:usedSkillTimes(self.name, Player.HistoryTurn) > 2 then
            room:loseHp(data.from,1,self.name)
        end
    end,
}
local zuogu_buff = fk.CreateProhibitSkill{
  name = "#tshu__zuogu_buff",
  frequency = Skill.Compulsory,
  is_prohibited = function(self, from, to, card)
      return to:hasSkill(self) and from.role == "lord"
  end,
}
zuogus:addRelatedSkill(zuogu_buff)
zuogu:addSkill(jisheng)
zuogu:addSkill(zuogus)
Fk:loadTranslationTable{
    ["tshulz__zuogu"] = "左鼓",
    
    ["tshu__jisheng"] = "奇声",
    [":tshu__jisheng"] = "锁定技，你跳过摸牌阶段。<br>奇数轮时，你每受到1点伤害后，主公失去1点体力，然后令一个随机类型的前进方式增加2米（每个类型每回合至多以此法增加10米）。"..
    "<br>偶数轮时，防止你受到的伤害。",
    ["tshu__zuogu"] = "左鼓",
    [":tshu__zuogu"] = "锁定技，你受到伤害后:<br>若此次为你第一次发动此技能，伤害来源摸两张牌；<br>若为你第二次发动此技能，伤害来源弃置俩张牌;<br>若为三次以上发动此技能，伤害来源失去一点体力。",

    ["$tshu__jisheng"] = "咚咚咚咚咚咚~",
}

local yougu = General(extension, "tshulz__yougu","qun",10,10,General.Female)
yougu.hidden = true
local ousheng = fk.CreateTriggerSkill{
    name = "tshu__ousheng",
    anim_type = "defensive",
    priority = 2,
    events = {fk.EventPhaseChanging,fk.Damaged,fk.DamageInflicted,fk.Death},
    frequency = Skill.Compulsory,
    can_trigger = function(self, event, target, player, data)
      if event == fk.EventPhaseChanging then
        return target == player and player:hasSkill(self)  and data.to == Player.Draw
      elseif event == fk.Damaged then
        return target == player and player:hasSkill(self,true,true) and player.room:getTag("RoundCount")%2 == 0
      elseif event == fk.DamageInflicted then
        return target == player and player:hasSkill(self) and player.room:getTag("RoundCount")%2 ~= 0
      end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
      if event == fk.EventPhaseChanging then
        return true
      elseif event == fk.Damaged  then
        local n =  data.damage 
        if n < 1 then return end
        for i = 1, n, 1 do
            for _, p in ipairs(room.alive_players) do
                if p.role == "lord" then
                    room:loseHp(p,1,self.name)
                    break
                end
            end
            local lz = {"lz_draw","lz_damage","lz_cover","lz_control"}
            local glz = {}
            for _, m in ipairs(lz) do
                if player:getMark(m.."-turn") < 9 then
                    table.insertIfNeed(glz,m)
                end
            end
            if #glz > 0 then
                local m = table.random(glz)
                room:addPlayerMark(player,m.."-turn",2)
                room:setBanner("@lzjd",((tonumber(string.match(room:getBanner("@lzjd") or 0, "%d+"))) + 2).."米")
                room:setBanner("lzjd",(room:getBanner("lzjd") or 0) + 2)
                room:setBanner(m,(room:getBanner(m) or 0) + 2)
                room.logic:trigger("fk.lzjd", player, { name = "@lzjd", num = 2})
            end
        end
      elseif event == fk.DamageInflicted then
        return true
      end
    end,
}
local yougus = fk.CreateTriggerSkill{
    name = "tshu__yougu",
    anim_type = "defensive",
    events = {fk.Damaged},
    frequency = Skill.Compulsory,
    can_trigger = function(self, event, target, player, data)
      return target == player and player:hasSkill(self) and data.from and not data.from.dead
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        if  player:usedSkillTimes(self.name, Player.HistoryTurn) == 1 then
            if data.from:isWounded() then
            room:recover({
                who = data.from,
                num = 1,
                recoverBy = player,
                skillName = self.name
              })
            end
        elseif  player:usedSkillTimes(self.name, Player.HistoryTurn) == 2 then
            room:loseHp(data.from,1,self.name)
        elseif player:usedSkillTimes(self.name, Player.HistoryTurn) > 2 then
            if not data.from:isNude() then
                room:askForDiscard(data.from,2,2,true,self.name,false)
            end
        end
    end,
}
local yougu_buff = fk.CreateProhibitSkill{
  name = "#tshu__yougu_buff",
  frequency = Skill.Compulsory,
  is_prohibited = function(self, from, to, card)
      return to:hasSkill(self) and from.role == "lord"
  end,
}
yougus:addRelatedSkill(yougu_buff)
yougu:addSkill(ousheng)
yougu:addSkill(yougus)
Fk:loadTranslationTable{
    ["tshulz__yougu"] = "右鼓",
    
    ["tshu__ousheng"] = "偶声",
    [":tshu__ousheng"] = "锁定技，你跳过摸牌阶段。<br>偶数轮时，你每受到1点伤害后，主公失去1点体力，然后令一个随机类型的前进方式增加2米（每个类型每回合至多以此法增加10米）。"..
    "<br>奇数轮时，防止你受到的伤害。",
    ["tshu__yougu"] = "右鼓",
    [":tshu__yougu"] = "锁定技，你受到伤害后:<br>若此次为你第一次发动此技能，伤害来源回复一点体力；<br>若为你第二次发动此技能，伤害来源失去一点体力;<br>若为三次以上发动此技能，伤害来源弃置两张牌。",

    ["$tshu__ousheng"] = "咚咚咚咚咚咚~",
    ["@lz_draw-turn"] = "摸牌",
    ["@lz_damage-turn"] = "伤害",
    ["@lz_cover-turn"] = "回复",
    ["@lz_control-turn"] = "控制",
}

local qingtian = fk.CreateTriggerSkill{
  name = "tshu__qingtian",
  mute = true,
  priority = 6,
  global = true,
  events = {fk.CardUsing},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player.room:getBanner("@[:]lzhou__tianqi") == self.name and player:getMark(self.name.."turn") < 4 and player.phase == Player.Play and player.phase ~= Player.NotActive
  end,
  on_use = function(self, event, target, player, data)
      local room = player.room
      room:addPlayerMark(player,self.name.."turn",1)
      room:setBanner("@lzjd",((tonumber(string.match(room:getBanner("@lzjd") or 0, "%d+"))) + 5).."米")
      room:setBanner("lzjd",(room:getBanner("lzjd") or 0) + 5)
      room.logic:trigger("fk.lzjd", player, { name = "@lzjd", num = 5})
  end,
}
Fk:addSkill(qingtian)
Fk:loadTranslationTable{
  ["tshu__qingtian"] = "晴天",
  [":tshu__qingtian"] = "当玩家于出牌阶段使用牌时，“龙舟进度”增加5米，每回合至多以此方式增加20米。"
}

local dawu = fk.CreateDistanceSkill{
  name = "tshu__dawu",
  global = true,
  frequency = Skill.Compulsory,
  correct_func = function(self, from, to)
    if Fk:currentRoom():getBanner("@[:]lzhou__tianqi") == self.name then
      if (from.role == "lord"  and to.role == "rebel" ) or (from.role == "rebel"  and to.role == "lord") then
        return 3
      end
    end
  end,
}
Fk:addSkill(dawu)
Fk:loadTranslationTable{
  ["tshu__dawu"] = "大雾",
  [":tshu__dawu"] = "玩家与BOSS互相计算距离+3。"
}

local lieri = fk.CreateTriggerSkill{
  name = "tshu__lieri",
  mute = true,
  priority = 6,
  global = true,
  events = {fk.TurnStart},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player.room:getBanner("@[:]lzhou__tianqi") == self.name and player.role == "rebel"
  end,
  on_use = function(self, event, target, player, data)
      local room = player.room
      local card = room:askForCard(player,1,1,false,self.name,true,"jink","烈日：弃置一张【闪】或受到一点火焰伤害。")
      if #card > 0 then
        room:moveCardTo(card,Card.DiscardPile,player,fk.ReasonDiscard)
      else
        room:damage({
          to = player,
          damage = 1,
          damageType = fk.FireDamage,
          skillName = self.name,
        })
      end
  end,
}
Fk:addSkill(lieri)
Fk:loadTranslationTable{
  ["tshu__lieri"] = "烈日",
  [":tshu__lieri"] = "玩家的回合开始时需要弃置一张【闪】，否则受到一点火焰伤害。"
}

local leidian = fk.CreateTriggerSkill{
  name = "tshu__leidian",
  mute = true,
  priority = 6,
  global = true,
  events = {fk.TurnStart},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player.room:getBanner("@[:]lzhou__tianqi") == self.name and player.role == "rebel"
  end,
  on_use = function(self, event, target, player, data)
      local room = player.room
      local no = true
      if #player:getCardIds("e") > 0 and room:askForSkillInvoke(player,self.name,nil,"雷电：弃置一张装备区的牌或点取消横置自身并受到一点雷电伤害。") then
        local id = room:askForCardChosen(player, player, "e", self.name)
        if id then
          room:throwCard({id}, self.name, player, player)
          no = false
        end
      end
      if no and not player.chained then
        player:setChainState(not player.chained)
      end
      if no then
        room:damage({
          to = player,
          damage = 1,
          damageType = fk.ThunderDamage,
          skillName = self.name,
        })
      end
  end,
}
Fk:addSkill(leidian)
Fk:loadTranslationTable{
  ["tshu__leidian"] = "雷电",
  [":tshu__leidian"] = "玩家的回合开始时需要弃置一张装备区的牌，否则横置自身并受到一点雷电伤害。"
}

local yintian = fk.CreateTriggerSkill{
  name = "tshu__yintian",
  mute = true,
  priority = 6,
  global = true,
  events = {fk.Damaged},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player.role == "lord" and  player.room:getBanner("@[:]lzhou__tianqi") == self.name 
  end,
  on_use = function(self, event, target, player, data)
      local room = player.room
      local judge = {
        who = player,
        reason = self.name,
        pattern = ".|.|heart",
      }
      room:judge(judge)
      if judge.card.suit == Card.Heart then
        room:recover({
          who = player,
          num = 1,
          recoverBy = player,
          skillName = self.name
        })
      end
  end,
}
Fk:addSkill(yintian)
Fk:loadTranslationTable{
  ["tshu__yintian"] = "阴天",
  [":tshu__yintian"] = "BOSS受到伤害后进行一次判定，若判定结果为♥，BOSS回复一点体力。",
}

--BOSS专属
local kuangfeng = fk.CreateTriggerSkill{
  name = "tshu__kuangfeng",
  mute = true,
  priority = 6,
  global = true,
  events = {fk.DamageInflicted},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player.role == "rebel" and  player.room:getBanner("@[:]lzhou__tianqi") == self.name 
  end,
  on_use = function(self, event, target, player, data)
      local room = player.room
      data.damage = data.damage + 1
  end,
}
Fk:addSkill(kuangfeng)
Fk:loadTranslationTable{
  ["tshu__kuangfeng"] = "狂风",
  [":tshu__kuangfeng"] = "玩家受到伤害时，伤害+1。",
}

local julang = fk.CreateProhibitSkill{
  name = "tshu__julang",
  prohibit_use = function(self, player, card)
      return player.role == "rebel" and Fk:currentRoom():getBanner("@[:]lzhou__tianqi") == self.name  and (card.name == "jink" or card.name == "analeptic")
  end,
  prohibit_response = function(self, player, card)
      return player.role == "rebel" and Fk:currentRoom():getBanner("@[:]lzhou__tianqi") == self.name  and (card.name == "jink" or card.name == "analeptic")
  end,
}
Fk:addSkill(julang)
Fk:loadTranslationTable{
  ["tshu__julang"] = "巨浪",
  [":tshu__julang"] = "玩家无法使用或打出“闪”,“酒”。",
}

local dayu = fk.CreateTriggerSkill{
  name = "tshu__dayu",
  mute = true,
  priority = 6,
  global = true,
  events = {fk.DrawNCards},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player  and  player.room:getBanner("@[:]lzhou__tianqi") == self.name 
  end,
  on_use = function(self, event, target, player, data)
      local room = player.room
      if player.role == "lord" then
        data.n = data.n + 2
      elseif player.role == "rebel" then
        data.n = data.n - 1
      end
  end,
}
Fk:addSkill(dayu)
Fk:loadTranslationTable{
  ["tshu__dayu"] = "大雨",
  [":tshu__dayu"] = "玩家的摸牌阶段摸牌数-1，BOSS的摸牌阶段摸牌数+2。",
}

local leiyu = fk.CreateTriggerSkill{
  name = "tshu__leiyu",
  mute = true,
  priority = 6,
  global = true,
  events = {fk.CardUsing},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player  and player.role == "rebel" and  player.room:getBanner("@[:]lzhou__tianqi") == self.name and data.card.type == Card.TypeBasic and not player:isKongcheng()
  end,
  on_use = function(self, event, target, player, data)
      local room = player.room
      local cards = player:getCardIds("h")
      room:moveCardTo(table.random(cards),Card.DiscardPile,player,fk.ReasonDiscard)
  end,
}
Fk:addSkill(leiyu)
Fk:loadTranslationTable{
  ["tshu__leiyu"] = "雷雨",
  [":tshu__leiyu"] = "玩家使用基本牌时，随机弃置一张手牌。",
}

--登神
local kuangfeng2 = fk.CreateTriggerSkill{
  name = "tshu__kuangfeng2",
  mute = true,
  priority = 6,
  global = true,
  events = {fk.DamageInflicted},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and (player.role == "lord" or player.role == "loyalist") and  player.room:getBanner("@[:]lzhou__tianqi") == self.name 
  end,
  on_use = function(self, event, target, player, data)
      local room = player.room
      data.damage = data.damage + 1
  end,
}
Fk:addSkill(kuangfeng2)
Fk:loadTranslationTable{
  ["tshu__kuangfeng2"] = "狂风",
  [":tshu__kuangfeng2"] = "除江神以外角色受到伤害时，伤害+1。",
}

local julang2 = fk.CreateProhibitSkill{
  name = "tshu__julang2",
  prohibit_use = function(self, player, card)
      return (player.role == "lord" or player.role == "loyalist") and Fk:currentRoom():getBanner("@[:]lzhou__tianqi") == self.name  and (card.name == "jink" or card.name == "analeptic")
  end,
  prohibit_response = function(self, player, card)
      return (player.role == "lord" or player.role == "loyalist") and Fk:currentRoom():getBanner("@[:]lzhou__tianqi") == self.name  and (card.name == "jink" or card.name == "analeptic")
  end,
}
Fk:addSkill(julang2)
Fk:loadTranslationTable{
  ["tshu__julang2"] = "巨浪",
  [":tshu__julang2"] = "除江神以外角色无法使用或打出“闪”,“酒”。",
}

local dayu2 = fk.CreateTriggerSkill{
  name = "tshu__dayu2",
  mute = true,
  priority = 6,
  global = true,
  events = {fk.DrawNCards},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player  and  player.room:getBanner("@[:]lzhou__tianqi") == self.name 
  end,
  on_use = function(self, event, target, player, data)
      local room = player.room
      if player.role == "rebel" then
        data.n = data.n + 2
      elseif (player.role == "lord" or player.role == "loyalist") then
        data.n = data.n - 1
      end
  end,
}
Fk:addSkill(dayu2)
Fk:loadTranslationTable{
  ["tshu__dayu2"] = "大雨",
  [":tshu__dayu2"] = "除江神以外的角色摸牌阶段摸牌数-1，江神的摸牌阶段摸牌数+2。",
}

local leiyu2 = fk.CreateTriggerSkill{
  name = "tshu__leiyu2",
  mute = true,
  priority = 6,
  global = true,
  events = {fk.CardUsing},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player  and (player.role == "lord" or player.role == "loyalist") and  player.room:getBanner("@[:]lzhou__tianqi") == self.name and data.card.type == Card.TypeBasic and not player:isKongcheng()
  end,
  on_use = function(self, event, target, player, data)
      local room = player.room
      local cards = player:getCardIds("h")
      room:moveCardTo(table.random(cards),Card.DiscardPile,player,fk.ReasonDiscard)
  end,
}
Fk:addSkill(leiyu2)
Fk:loadTranslationTable{
  ["tshu__leiyu2"] = "雷雨",
  [":tshu__leiyu2"] = "除江神以外的角色使用基本牌时，随机弃置一张手牌。",
}
return extension